<template>
	<view class="container">
		<view class="form-container">
			<u--form :model="list" ref="uForm">
				<u-form-item label="测试账户" prop="list.userName" borderBottom ref="item1">
					<u--input v-model="list.userName" border="none" placeholder="请输入用户名"></u--input>
				</u-form-item>

				<u-form-item label="年龄" prop="list.age" borderBottom>
					<u--input v-model="list.age" border="none" placeholder="请输入年龄"></u--input>
				</u-form-item>

				<u-form-item label="性别" prop="list.sex" borderBottom>
					<u-radio-group v-model="list.sex" placement="row">
						<u-radio v-for="(item, index) in radiolist1" :key="index" :label="item.text" :name="item.value"
							:customStyle="{marginRight: '20px'}"></u-radio>
					</u-radio-group>
				</u-form-item>

				<u-form-item label="出生日期" prop="list.birthDay" borderBottom>
					<u--input v-model="list.birthDay" border="none" placeholder="请选择出生日期" disabled
						disabledColor="#ffffff" @click="showDatePicker = true"></u--input>
					<u-datetime-picker :show="showDatePicker" v-model="birthDayValue" mode="date" @confirm="dateConfirm"
						@cancel="showDatePicker = false"></u-datetime-picker>
				</u-form-item>

				<u-form-item label="手机" prop="list.phone" borderBottom>
					<u--input v-model="list.phone" border="none" placeholder="请输入手机号"></u--input>
				</u-form-item>

				<u-form-item label="年级" prop="list.userLevel" borderBottom>
					<u--input v-model="gradeName" border="none" placeholder="请选择年级" disabled disabledColor="#ffffff"
						@click="showGradePicker = true"></u--input>
					<u-picker :show="showGradePicker" :columns="gradeOptions" keyName="label" @confirm="gradeConfirm"
						@cancel="showGradePicker = false"></u-picker>
				</u-form-item>
			</u--form>

			<view class="submit-btn">
				<u-button text="保存" type="primary" @click="submitForm" :customStyle="{marginTop: '40px'}"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import http from '../../http'
	export default {
		data() {
			return {
				showDatePicker: false,
				birthDayValue: Number(new Date()),
				showGradePicker: false,
				gradeName: '初二',
				gradeOptions: [
					[{
							label: '初一',
							value: 1
						},
						{
							label: '初二',
							value: 2
						},
						{
							label: '初三',
							value: 3
						},
						{
							label: '高一',
							value: 4
						},
						{
							label: '高二',
							value: 5
						},
						{
							label: '高三',
							value: 6
						},
					]
				],
				radiolist1: [{
						text: '男',
						value: 1,
					},
					{
						text: '女',
						value: 2
					}
				],
				list: {
					id: "",
					userUuid: "",
					userName: "", //用户名
					realName: "Test", //真实姓名
					age: "", //年龄
					role: "", //角色
					sex: 1, //性别 默认男
					birthDay: "2019-08-15 00:00:00", //生日
					phone: "", //手机号
					lastActiveTime: "",
					createTime: "",
					modifyTime: "",
					status: "", //状态
					userLevel: 2, //年级 默认初二
					classes: "", //用户班级
					imagePath: "" //用户头像
				}
			}
		},
		methods: {
			getList() {
				http({
					url: '/api/student/user/current',
					method: 'POST',
				}).then(res => {
					console.log(res);
					if (res.code === 1) {
						this.list = res.response
						// 设置年级显示
						const grade = this.gradeOptions[0].find(item => item.value === this.list.userLevel)
						if (grade) {
							this.gradeName = grade.label
						}
					}
				}).catch(err => {
					console.error(err)
				})
			},

			dateConfirm(e) {
				const date = new Date(e.value)
				const year = date.getFullYear()
				const month = (date.getMonth() + 1).toString().padStart(2, '0')
				const day = date.getDate().toString().padStart(2, '0')
				this.list.birthDay = `${year}-${month}-${day} 00:00:00`
				this.showDatePicker = false
			},

			gradeConfirm(e) {
				this.list.userLevel = e.value[0].value
				this.gradeName = e.value[0].label
				this.showGradePicker = false
			},

			submitForm() {
				http({
					url: '/api/student/user/update',
					method: 'POST',
					data: this.list
				}).then(res => {
					console.log(res)
					if (res.code === 1) {
						uni.showToast({
							title: '保存成功',
							icon: 'success',
						})
						setTimeout(() => {
							uni.switchTab({
								url: '/pages/studentpage/My', // 修正后的路径
								fail: (err) => {
									console.log('跳转失败：', err) // 调试用
								}
							})
						}, 1500)
					} else {
						uni.showToast({
							title: res.message || '保存失败',
							icon: 'none',
						})
					}
				}).catch(err => {
					console.error(err)
					uni.showToast({
						title: '保存失败',
						icon: 'none'
					})
				})
			}
		},
		onLoad() {
			this.getList()
		}
	}
</script>

<style>
	.container {
		padding: 20px;
		background-color: #f8f8f8;
		min-height: 100vh;
	}

	.form-container {
		background-color: #ffffff;
		border-radius: 10px;
		padding: 15px;
		box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
	}

	.submit-btn {
		margin-top: 30px;
	}

	.u-form-item {
		padding: 12px 0;
	}
</style>